package cn.wildfire.chat.kit.user;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import butterknife.BindView;
import butterknife.OnTextChanged;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.common.OperateResult;
import cn.wildfire.chat.kit.third.utils.UIUtils;

/**
 * Create by HEKE, 2019/8/29 19:22
 * TODO  设置备注信息界面
 */
public class SetAliasActivity extends WfcBaseActivity {

    private String userId;
    //private Friend mFriend;

    @BindView(R2.id.aliasEditText)
    EditText aliasEditText;

    private MenuItem menuItem;
    private UserViewModel userViewModel;
    String alias;

    @Override
    protected int contentLayout() {
        return R.layout.contact_set_alias_activity;
    }

    @Override
    protected void afterViews() {
        userId = getIntent().getStringExtra("userId");
        if (TextUtils.isEmpty(userId)) {
            finish();
            return;
        }
        userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        alias = userViewModel.getFriendAlias(userId);
        if (!TextUtils.isEmpty(alias)) {
            aliasEditText.setText(alias);
        } else {
            aliasEditText.setHint(R.string.alias);
        }
        try {
            /**
             * modify by HEKE , 2019/8/29 19:25 TODO 修改设置备注内容
             */
            setToolbarOrReplaceType(true, -1);
            setReplaceTitle(null, R.string.set_remark);
            RelativeLayout toolbarLayout = getReplaceViewsLayout();
            // 取消
            TextView cancel = new TextView(getApplicationContext());
            cancel.setText(getString(R.string.cancle_but));
            RelativeLayout.LayoutParams txtLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            cancel.setLayoutParams(txtLayout);
            cancel.setGravity(Gravity.CENTER_VERTICAL);
            int padding = cn.wildfire.chat.app.extendsion.utils.TextUtils.dip2px(this, 16);
            cancel.setPadding(padding, 0, 0, 0);
            cancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            cancel.setTextColor(Color.BLACK);
            toolbarLayout.addView(cancel);
            // 确定按钮
            getRightTxtView().setBackgroundResource(R.drawable.commom_green_radiusmuch);
            getRightTxtView().setTextColor(getResources().getColor(R.color.white));
            setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.done_title), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    changeAlias();
                }
            });

            // 绑定事件
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setSearchLayoutVisibility(false);
    }

    @Override
    protected int menu() {
        return R.menu.user_set_alias;
    }

    @Override
    protected void afterMenus(Menu menu) {
        menuItem = menu.findItem(R2.id.save);
        menuItem.setEnabled(false);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R2.id.save) {
            changeAlias();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnTextChanged(R2.id.aliasEditText)
    void onAliasEditTextChange() {
        menuItem.setEnabled(aliasEditText.getText().toString().trim().length() > 0 ? true : false);
    }

    private void changeAlias() {
        String displayName = aliasEditText.getText().toString().trim();
        String hintString = aliasEditText.getHint().toString().trim();
        if (hintString.equals(getResources().getString(R.string.alias)) && TextUtils.isEmpty(displayName)) {
            UIUtils.showToast(UIUtils.getString(R.string.alias_no_empty));
            return;
        }

        if (displayName.equals(alias)) {
            UIUtils.showToast(UIUtils.getString(R.string.alias_modify_fuailre));
            return;
        }

        userViewModel.setFriendAlias(userId, displayName).observe(this, new Observer<OperateResult<Integer>>() {
            @Override
            public void onChanged(OperateResult<Integer> integerOperateResult) {
                if (integerOperateResult.isSuccess()) {
                    Toast.makeText(SetAliasActivity.this, getString(R.string.modify_success), Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(SetAliasActivity.this, getString(R.string.modify_alias_error) + "：" + integerOperateResult.getErrorCode(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
